package beans;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.faces.event.AjaxBehaviorEvent;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

public class CompanyActivities {
	
	public static class Activity
	{
		private String activityName;
		private int primaryKey;
		
		public Activity(String activityName, int primaryKey) {
			this.activityName = activityName;
			this.primaryKey = primaryKey;
		}

		public Activity() {
		}

		public String getActivityName() {
			return activityName;
		}

		public void setActivityName(String activityName) {
			this.activityName = activityName;
		}

		public int getPrimaryKey() {
			return primaryKey;
		}

		public void setPrimaryKey(int primaryKey) {
			this.primaryKey = primaryKey;
		}
		
		
		
	}

	private BasicDataSource ds;
	private List<CompanyActivities.Activity> activities;
	private ArrayList<CompanyActivities.Activity> types;
	
	//@ManagedProperty(value="#{c.primaryKey}")
	private Integer selectedPK;


	private int typeSelected;;
	

	public CompanyActivities(BasicDataSource ds)
	{
		this.ds = ds;
		activities = new ArrayList<CompanyActivities.Activity>();
		types = new ArrayList<CompanyActivities.Activity>();
		loadActivities();
		//activities.add(new Activity("AAA", 1));
	}
	
	private void loadActivities()
	{
		Connection cnn = null;
		Statement stat = null;
		ResultSet rs = null;
		
		try {
			cnn = ds.getConnection();
			stat = cnn.createStatement();
			String getAllActivities = "SELECT * FROM companyactivity";
			rs = stat.executeQuery(getAllActivities);
			while(rs.next())
			{
				String n = rs.getString("activityName");
				int p = rs.getInt("idCompanyActivity");
				Activity a = new Activity(n, p);
				activities.add(a);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			if( cnn != null )
			{
				try {
					cnn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(stat != null)
			{
				try {
					stat.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if(rs != null)
			{
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	public void fillTypes(AjaxBehaviorEvent a)
	{
		Connection cnn = null;
		Statement stat = null;
		ResultSet rs = null;
		types.clear();
		
		try {
			cnn = ds.getConnection();
			stat = cnn.createStatement();
			rs = stat.executeQuery("SELECT * FROM companytype WHERE idCompanyActivity = '" + selectedPK + "'");
			while(rs.next())
			{
				String typeName = rs.getString("typeName");
				int compType = rs.getInt("idCompanyType");
				types.add(new Activity(typeName, compType));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			if(cnn != null)
			{
				try {
					cnn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if(stat != null)
			{
				try {
					stat.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if(rs != null)
			{
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public List<CompanyActivities.Activity> getActivities() {
		return activities;
	}

	public void setActivities(ArrayList<CompanyActivities.Activity> activities) {
		this.activities = activities;
	}

	public ArrayList<CompanyActivities.Activity> getTypes() {
		return types;
	}

	public void setTypes(ArrayList<CompanyActivities.Activity> types) {
		this.types = types;
	}

	

	public Integer getSelectedPK() {
		return selectedPK;
	}

	public void setSelectedPK(Integer selectedPK) {
		this.selectedPK = selectedPK;
	}

	public void setActivities(List<CompanyActivities.Activity> activities) {
		this.activities = activities;
	}

	public int getTypeSelected() {
		return typeSelected;
	}

	public void setTypeSelected(int typeSelected) {
		this.typeSelected = typeSelected;
	}
	
	
	
}
